package GameElement;


import java.io.IOException;
import java.util.Scanner;

public class GameIO {
	Scanner sc;					// Define system input
	
	/**
	 * Initialize the system input
	 * @param sc
	 */
	public GameIO(Scanner sc){
		this.sc = sc;
	}
	
	/**
	 * Check the import txt file of card list is valid or not
	 * if not, it will output the error message and exit program 
	 * @param cardExist
	 * @param source
	 */
	public void validateCardtxt(boolean cardExist, String source){
		if(!cardExist){
			System.out.print("Cannot find "+source+"\n");
			System.exit(0);
		}
	}
	
	/**
	 * Return the number of game players in the game
	 * @return number of game players
	 * @throws IOException
	 */
	public int getNumberOfPlayers() throws IOException{
		int numberOfPlayers = 0;
		while(true){
			try{
				System.out.print("Enter the number of players(At least 2): ");
				numberOfPlayers = sc.nextInt();
				if(numberOfPlayers < 2){
					warnInvalidInput();
				}else return numberOfPlayers;
			}catch(Exception e){
				warnInvalidInput();
				sc.next();
			}
		}
	}
	
	/**
	 * Return game player name, refer to the index of the queue
	 * @param i
	 * @return player name
	 */
	public String getPlayersName(int i){
		System.out.print("Enter the name of player " + (i + 1) + ": ");
		return sc.next();
	}
	
	/**
	 * Return game player role, refer to the index of the queue
	 * @param i
	 * @return player role
	 */
	public int getRole(String roleList[]) throws IOException{
		int role = 0;
		while(true){
			for (int j = 1; j <= roleList.length; j++) {
				System.out.print(j + ". " + roleList[j - 1] + "\n");
			}
			System.out.print("Please select the roles below: ");
			try{
				role = sc.nextInt();
				if(role < 1 || role > 6){
					warnInvalidInput();
				}else
					return role-1;
			}catch(Exception e){
				warnInvalidInput();
				sc.next();
			}
		}
	}
	
	/**
	 * Return player selected options in integer presented.
	 * If error inpurt, it will display the error input message.
	 * @param current_player
	 * @return player selected options
	 * @throws IOException
	 */
	public int getActionOfOptionMenu(GamePlayer current_player) throws IOException{
		int option = 0;
		while(true){
			try{
				System.out.print("Now is " + current_player.getGamePlayerName()
						+ "'s turn\n");
				String[] options = { "Roll Dice", "Pass (deduct 50 scores)" };
				for (int i = 1; i <= 2; i++) {
					System.out.print(i + ". " + options[i - 1] + "\n");
				}
				System.out.print("Your choice is? ");
				option = sc.nextInt();
				if(option != 1 && option != 2){
					warnInvalidInput();
				}else
					return option;
			}catch(Exception e){
				warnInvalidInput();
				sc.next();
			}
		}
	}
	
	/**
	 * Display the incorrect input message.
	 */
	public void warnInvalidInput(){
		System.out.print("Incorrect Input! Please Retry!\n");
	}
	
	/**
	 * Display the game win message according to the goal score.
	 * @param goalScore
	 */
	public void printWinGame(int goalScore) {
		System.out.print("You all got more then " + goalScore + " scores!\n");
		System.out.print("Congratulation, you won!\n");
	}
	
	/**
	 * Display the game lose message by which player.
	 * @param losePlayerName
	 */
	public void printLoseGame(String losePlayerName) {
		System.out.print("Sorry! " + losePlayerName + "'s score is less then 0!\n");
		System.out.print("Game over, you lost!\n");
	}
	
	/**
	 * Display the end turn message.
	 * @param playerName
	 */
	public void printEndTurn(String playerName){
		System.out.print("\n"+playerName + " end the turn!!\n\n");
	}
	
	/**
	 * Display the event executed.
	 * @param playerPosition
	 */
	public void printEvent(int playerPosition){
		int position[] = {0,10,20,30};
		String eventDesc[] = {"Congratulation, you have passed a round (Bonus points 300)\n",
				"A strong wind blow you forward. (Move forward to position 13)\n",
				"Sorry, you have been arrested. (Deduct 100 scores)\n",
				"A strong wind blow you backward. (Move backward to position 27)\n",
				"You step into an event!!!\n"};
		for(int i=0; i<position.length; i++){
			if(playerPosition==position[i]){
				System.out.print(eventDesc[i]);
				return;
			}
		}
		System.out.print(eventDesc[4]);
	}
	
	/**
	 * Get the player answer from the act card question
	 * @param c
	 * @return player input of act card answer
	 */
	public String getActCardAnswerFromPlayer(GameCard c){
		String answer;
		while(true){
			System.out.print("Act Card:\n"+c.getCardName() + ": " + c.getCardDescription() + "\nAccept?(A) Reject?(R) Please enter \"A\" or \"R\" : ");
			answer = sc.next();
			if(!answer.equals("A") && !answer.equals("R")){
				warnInvalidInput();
			}else return answer;
		}
	}
	
	/**
	 * Get the player answer from the act card question.
	 * According to player role and have the different answer and score
	 * @param c
	 * @param role
	 * @return
	 */
	public int getEventCardAnswerFromPlayer(GameCard c, int role) {
		int answer = 0;
		while(true){
			try{
				System.out.print("Event Card:\n"+c.getCardName() + ": " + c.getCardDescription() + "\n");
				for(int i=0; i<4; i++){
					System.out.print((i+1)+". "+c.getCardAnswer()[role][i] + "\n");
				}
				System.out.print("Plase choose a most suitable answer. (e.g. type \"1\" for choosing answer 1) : ");
				answer = sc.nextInt();
				if(answer < 1 || answer > 4){
					warnInvalidInput();
				}else 
					return answer-1;
			}catch(Exception e){
				warnInvalidInput();
				sc.next();
			}
		}
		
	}
	
	/**
	 * Get the player answer from the act card question
	 * @param c
	 * @return player input of act card answer
	 */
	public String getSayCardAnswerFromPlayer(GameCard c){
		String answer;
		while(true){
			System.out.print("Say Card:\n"+c.getCardName() + ": " + c.getCardDescription() + "\nAccept?(A) Reject?(R) Please enter \"A\" or \"R\" : ");
			answer = sc.next();
			if(!answer.equals("A") && !answer.equals("R")){
				warnInvalidInput();
			}else return answer;
		}
	}
	
	/**
	 * Get the other players feedback.
	 * @param c
	 * @param currentPlayer
	 * @param game_players
	 * @param player_num
	 * @return other players feedback message
	 */
	public String getOtherPlayersFeedback(GameCard c, GamePlayer currentPlayer, GamePlayer[] game_players, int player_num){
		String answer;
		while(true){
			System.out.print("Have you give comment or advice to "+currentPlayer.getGamePlayerName()+", "+game_players[player_num].getGamePlayerName()+"? Yes?(Y) No?(N), Please enter \"Y\" or \"N\" : ");
			answer = sc.next();
			if(!answer.equals("Y") && !answer.equals("N")){
				warnInvalidInput();
			}else return answer;
		}
	}
	
	/**
	 * player answer wrongly, deducted player score.
	 * @param score
	 */
	public void printChoseWrongAnswer(int score){
		System.out.print("Sorry, your score is deducted by "+score+"\n");
	}
	
	/**
	 * player answer correctly, add player score.
	 * @param score
	 */
	public void printChoseCorrectAnswer(int score){
		System.out.print("Congratulation, your score is added by "+score+"\n");
	}
	
	/**
	 * Display dice and player position after rolled dice
	 * @param score
	 */
	public void printDiceAndPosition(int dicePoints, int playerPosition){
		System.out.print("\nDice point: "+ dicePoints + "\n");
		System.out.print("Your position now is "+playerPosition+"\n\n");
	}
	
}
